How can print multidimensional array of single array.
$data = array(
'ride_categry'=>$ridecategory,
'pool_ride_type'=> $pollride,
'ride_looking_for'=> $ridefor,
'seating_capacity'=>$seatingcapcity,
'ride_from'=> $rideform,
'ride_to'=> $rideto,
'daily_route'=> $daily,
//'ride_now'=> $ridenow,
'ride_date'=> $ridedate,
'ride_time'=> $ridetime,
);
print this in multidimensional array like
Array
(
[one] => Array
(
[two] => Array
(
[three] => Array
(
[four] =>
)
)
)
)
PHP arrays are not multi-dimensional, they are nested. The example you gave does not illustrate a nested array.
If you want to exchange compound data structures between PHP and Javascript then use the builtin JSON routines:
<?php
$data=array(...);
header("Content-Type: application/json");
print json_encode($data);
If you want to print all the nested data from $data use print_r function:
print_r($data);
Related
I have a set of values as a string, like so:
MyCustomProductID1
MyCustomProductID2
Then I proceed to create an array out of these values with explode( "\n", $myProductIdString)
Now I have additional data (strings) that I want to combine with the value from my first array. I want that simple array into a multidimensional one:
Array
(
[0] => Array
(
[id] => MyCustomProductID1
[url] => http://example.com/MyCustomProductID1.jpg
)
[1] => Array
(
[id] => MyCustomProductID2
[url] => http://example.com/MyCustomProductID2.jpg
)
)
How do I get that first array into a multidimensional one and push data along with it?
Instead of direct assigning values to array use loop-
<?php
$str = "MyCustomProductID1
MyCustomProductID2";
$arr = explode("\n", $str);
$result = [];
$url_array = ["http://example.com/MyCustomProductID1.jpg", "http://example.com/MyCustomProductID2.jpg"];
for($i=0;$i<count($arr);$i++){
$result[$i]['id'] = $arr[$i];
$result[$i]['url'] = $url_array[$i];
}
print_r($result);
?>
I am trying to pull dynamic element in single array but result is not showing properly
Ex:
$array =array();
$element="'abc1','abc2'";
$array=array('abc',$element);
//I want result like that:
array[
[0]=>abc,
[1]=>abc1,
[2]=>ab
]
If you need to parse a string of elements to an array you can use one of the csv functions. You may then merge your arrays.
$array = array('abc');
$string_elements = "'abc1','abc2'";
$array_elements = str_getcsv($string_elements, ',', "'");
$array = array_merge($array, $array_elements);
var_export($array);
Output:
array (
0 => 'abc',
1 => 'abc1',
2 => 'abc2',
)
Alternatively to add each array element to the end of another array you can push them like so using a splat:
array_push($array, ...$array_elements);
According to my understanding, $element is storing string not an array so even if you try to get output it will display in following format
Array
(
[0] => abc
[1] => 'abc1','abc2'
)
Instead of this you can store array in $element variable and use array_merge() function to get required output e.g.
$element = array('abc1','abc2');
$array = array('abc');
$result = array_merge($array,$element);
// Output
Array
(
[0] => abc
[1] => abc1
[2] => abc2
)
I'm getting a response from a service and print_r() shows an array but it seems to be different from usual arrays.
If I print array_values it's are empty.
PHP:
print_r($token);
//Result: Array ( [{"access_token":"123","token_type":"bearer"}] => )
print_r(array_values($token));
//Result: Array ( [0] => )
Why are the access_token and token_type values not listed in array_values?
The answer is not JSON, It's not because you have a JSON type array. It is because there is NO value in your array.
//Result: Array ( [{"access_token":"123","token_type":"bearer"}] => )
That array has one index with no value, hence array_values shows nothing. You have created that array incorrectlly :)
I can reproduce your outputs with this:
$token = array('{"access_token":"123","token_type":"bearer"}' => '');
That is because you have an array with the key
'{"access_token":"123","token_type":"bearer"}'
but no value.
To access the JSON string in the array key, you could do this:
$keys = array_keys($token);
print_r($keys[0]);
To access the JSON object, you can further do
print_r(json_decode($keys[0]));
Output:
(
[access_token] => 123
[token_type] => bearer
)
Demo: Fiddle
I did a print_r on my array $total and it returned the following:
Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => stdClass Object (
[generated] => 6 [magnitude] => 3 [log_pk] => 14 [result] => 0.5000 ) )
Array ( ) Array ( )
I need to be able to print out log_pk from within the stdClass Object. I have tried print $total[0]->log_pk but that was unsuccessful. The error was Undefined offset: 0. Any help is appreciated. Thanks.
So this is within a loop, you should check if the index 0 exists first.
if (isset($total[0])) echo $total[0]->log_pk
Are you doing this within a loop? If so then it looks like the array is empty on most iterations. Try:
if (!empty($total)) print $total[0]->log_pk;
var_dump() displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
var_dump($total)
PHP: var_dump - Manual
it looks like your print_r is inside a loop.
while(true){
$total = some_function();
print_r($total);
if($condition) break;
}
// Here - outside the loop, is not the right place for the print_r();
If you want you print outside the loop, you would change $total = some_function(); to $total[] = some_function(); and then you can do print_r($total[index])
I have two arrays, one is generated by using explode() on a comma separated string and the other is generated from result_array() in Codeigniter.
The results when doing print_r are:
From explode():
Array
(
[0] => keyword
[1] => test
)
From database:
Array
(
[0] => Array
(
[name] => keyword
)
[1] => Array
(
[name] => test
)
)
I need them to match up so I can use array_diff(), what's the best way to get them to match? Is there something other than result_array() in CI to get a compatible array?
You could create a new array like this:
foreach($fromDatabase as $x)
{
$arr[] = $x['name'];
}
Now, you will have two one dim arrays and you can run array_dif.
$new_array = array();
foreach ($array1 as $line) {
$new_array[] = array('name' => $line);
}
print_r($new_array);
That should work for you.