Asked  7 Months ago    Answers:  5   Viewed   30 times

Is it possible to echo or print the entire contents of an array without specifying which part of the array?

The scenario: I am trying to echo everything from:

while($row = mysql_fetch_array($result)){
echo $row['id'];
}

Without specifying "id" and instead outputting the complete contents of the array.

 Answers

20

If you want to format the output on your own, simply add another loop (foreach) to iterate through the contents of the current row:

while ($row = mysql_fetch_array($result)) {
    foreach ($row as $columnName => $columnData) {
        echo 'Column name: ' . $columnName . ' Column data: ' . $columnData . '<br />';
    }
}

Or if you don't care about the formatting, use the print_r function recommended in the previous answers.

while ($row = mysql_fetch_array($result)) {
    echo '<pre>';
    print_r ($row);
    echo '</pre>';
}

print_r() prints only the keys and values of the array, opposed to var_dump() whichs also prints the types of the data in the array, i.e. String, int, double, and so on. If you do care about the data types - use var_dump() over print_r().

Wednesday, March 31, 2021
 
SheppardDigital
answered 7 Months ago
69

Convert the JSON to a php array

/* use json_decode(..., true) for creating an array, not an object of StdClass */
$array = json_decode($json, true);

Then create a temproary (or not temprary) array which contains all the contract_no and add the contents of each array element if the contract_no is the same

$temp_array = array();
foreach($array as $a){
    if(!isset($temp_array[$a['contract_no']])){
        $temp_array[$a['contract_no']] = array($a);
    }
    else{
        array_push($temp_array[$a['contract_no']], $a);
    }
}

This code assumes that all elements of the $array are arrays and all elements of the $arrays elements have the contract_no index. If this is not the case you can just use isset to check if the indices exist.

If you do not want the associative array you can just use

$result = array_values($temp_array)

So the $result array (or the $temp_array) are the Array-I and Array-II of your example stored in one array. I think this is easier. If you still want to have separate arrays (which i would not recommend) you can do something like this:

$array_counter = 1;
foreach($result as $r){
    $array_name = "array".$array_counter;
    $$array_name = $r;
    $array_counter++;
}

Edit

Even though this is the exact same code try this (I added some more error checking):

$temp_array = array();
foreach($array as $a){
    if(isset($a['contract_no']) && !empty($a['contract_no'])){
        if(!isset($temp_array[$a['contract_no']]) || !is_array($temp_array[$a['contract_no']])){
            $temp_array[$a['contract_no']] = array();
        }

        array_push($temp_array[$a['contract_no']], $a);
    }
}

It seems like you are setting the $temp_array[$a['contract_no']] = $a, this would cause the wrong format you are posting. The changed lines of code should make sure that the $a is added to an empty array (even though the code before worked for me and is like I said practicly the same)

This example can be seen in php sandbox here.

Wednesday, March 31, 2021
 
Alix
answered 7 Months ago
39

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;
    }

    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        }
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);
    }
}

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($dataArray,null,"A2");
Friday, May 28, 2021
 
Wilk
answered 5 Months ago
45

Here is one way to do this using array_walk_recursive:

array_walk_recursive($superheroes_empty, function(&$v, $k) use ($superheroes_complete) {
    if ($v === '' && isset($superheroes_complete[$k])) {
        $v = $superheroes_complete[$k];
    }
});

This will fill in any empty values if a corresponding key is found in $superheroes_complete. Since this makes replacements by reference, it will directly change the $superheroes_empty array, so if you still need the one with empty values, make a copy before using this.

Saturday, May 29, 2021
 
Kwadz
answered 5 Months ago
13

There's a lot been written about PHPExcel and memory use, and I'm not going to repeat it all here.

Try reading some of the threads on the PHPExcel discussion board discussing the issue, such as this one; or previous answers here on SO such as this one or this one

Tuesday, July 6, 2021
 
Slinky
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :