php error accessing double layer array - php

My data is in a second tier of a return value that I am trying to access the individual elements. Array ( [success] => 1 [return] => Array ( [0] => Array
I tried ['return'] since it is the key of that, is what I need but got the same error or obvious worse.
error:
Array to string conversion in orders.php on line 10
code:
<?php
$id = $argv[1]; //variable for inbound
require_once('phpPlay.php');
$result = api_query("mytrades", array("marketid" => $id));
foreach( $result as $x) {
echo $x;
}
?>
data top 5 rows:
Array
(
[success] => 1
[return] => Array
(
[0] => Array
(
[tradeid] => 74038377
[tradetype] => Sell
[datetime] => 2014-11-12 16:05:32
[tradeprice] => 0.00675000
[quantity] => 22.18670000
[fee] => -0.00007488
[total] => 0.14976023
[initiate_ordertype] => Buy
[order_id] => 197009493
)
[1] => Array
(
[tradeid] => 73687280
[tradetype] => Buy
[datetime] => 2014-11-09 03:38:13
[tradeprice] => 0.00816988
[quantity] => 0.00100000
[fee] => 0.00000002
[total] => 0.00000817
[initiate_ordertype] => Buy
[order_id] => 194824864
)
[2] => Array
(
[tradeid] => 73684313
[tradetype] => Sell
[datetime] => 2014-11-09 02:57:41
[tradeprice] => 0.00808034
[quantity] => 0.00100000
[fee] => 0.00000000
[total] => 0.00000808
[initiate_ordertype] => Buy
[order_id] => 194803992
)
[3] => Array
(
[tradeid] => 73653019
[tradetype] => Sell
[datetime] => 2014-11-08 17:53:12
[tradeprice] => 0.00793991
[quantity] => 0.00010000
[fee] => 0.00000000
[total] => 0.00000079
[initiate_ordertype] => Buy
[order_id] => 194559503
)
[4] => Array
(
[tradeid] => 73652717
[tradetype] => Sell
[datetime] => 2014-11-08 17:50:13
[tradeprice] => 0.00793989
[quantity] => 0.00100000
[fee] => 0.00000002
[total] => 0.00000794
[initiate_ordertype] => Sell
[order_id] => 194559596
)
...

The problem is that with the echo $x statement in your code above, $x is an array, not a string.
The echo function requires a string, so the error you're getting is because PHP is automatically trying to convert the parameter passed to echo to a string, but it is failing because you are passing an array.
Try this:
if (isset($result['return'])) {
foreach($result['return'] as $result_item) {
echo(var_export($result_item, true));
}
} else {
echo 'No results';
}

Related

One array in multiple array not showing values

I got this array when I encoded and decoded xml string with json.
[Main] => Array (
[0] => Array (
[ID] => 12345
[MemberID] => 12345
[PayerID] => 12345
[Gross] => 255.35
[Discount] => .00
[Net] => 255.35
[Encounter] => Array (
[Type] => 1
[Start] => 08/12/2019 09:58
[End] => 08/12/2019 10:28
[StartType] => 1
[EndType] => 1
)
[abcde] => Array (
[0] => Array (
[Type] => Principal
[Code] => 1234
)
[1] => Array (
[Type] => Secondary
[Code] => 1234
)
)
[Activity] => Array (
[0] => Array (
[ID] => 456789
[Type] => 5
[Code] => 0095-1234
[Quantity] => 2
[Net] => 83.00
)
[1] => Array (
[ID] => 3432091
[Type] => 5
[Code] => 0496-56789
[Quantity] => 1
[Net] => 19.95
)
)
)
[1] => Array (
[ID] => 12345
[MemberID] => 12345
[PayerID] => 12345
[Gross] => 255.35
[Discount] => .00
[Net] => 255.35
[Encounter] => Array (
[Type] => 1
[Start] => 08/12/2019 09:58
[End] => 08/12/2019 10:28
[StartType] => 1
[EndType] => 1
)
[abcde] => Array (
[0] => Array (
[Type] => Principal
[Code] => 1234
)
[1] => Array (
[Type] => Secondary
[Code] => 1234
)
)
[Activity] => Array (
[ID] => 456789
[Type] => 5
[Code] => 0095-1234
[Quantity] => 2
[Net] => 83.00
[item] => Array (
[0] => Array (
[Type] => value
[Code] => 4576878
[Value] => 34456
)
[1] => Array (
[Type] => value
[Code] => 4576878
[Value] => 34456
)
[2] => Array (
[Type] => value
[Code] => 4576878
[Value] => 34456
)
)
)
)
)
I have this array. But when I tried to echo values, Activity array in the $newArr['Main'][1]['Activity'][0] array is not showing values. When I tried to count the number of activity array, it is showing 6. But there is only one Activity array. I am getting values when I manually call the values with $newArr['Main'][1]['Activity']['ID'].
$new = simplexml_load_string($real_decoded_xml);
$con = json_encode($new);
$newArr = json_decode($con, true);
$ClaimArray = $newArr['Main'];
$main_array_count = sizeof($newArr['Main']);
for($i=0;$i<$main_array_count;$i++)
{
echo $ClaimArray[$i]["ID"].'<br>';
echo $ClaimArray[$i]["IDPayer"].'<br>';
echo $ClaimArray[$i]["MemberID"].'<br>';
echo $ClaimArray[$i]["Gross"].'<br>';
echo $ClaimArray[$i]["Net"].'<br>';
$abcdeArray = $ClaimArray[$i]['abcde'];
$abcdeArray_count = sizeof($abcdeArray);
for($d=0;$d<$abcdeArray_count;$d++)
{
echo $abcdeArray[$d]["Type"].'<br>';
echo $abcdeArray[$d]["Code"].'<br>';
}
$ActivityArray= $ClaimArray[$i]['Activity'];
$ActivityArray_count = sizeof($ActivityArray);
for($c=0;$c<$ActivityArray_count;$c++)
{
echo $ActivityArray[$c]["ID"].'<br>';
echo $ActivityArray[$c]["Type"].'<br>';
echo $ActivityArray[$c]["Code"].'<br>';
echo $ActivityArray[$c]["Quantity"].'<br>';
echo $ActivityArray[$c]["Net"].'<br>';
$itemArray = $ActivityArray[$c]['item'];
$item_count = sizeof($itemArray);
for($o=0;$o<$item_count;$o++)
{
echo $itemArray[$o]["Type"].'<br>';
echo $itemArray[$o]["Code"].'<br>';
echo $itemArray[$o]["Value"].'<br>';
}
}
}
Please help me to solve this.

Rearrange items in multidimensional array

I have a multidimensional array, I want to take the key 'data' from each array item and form another array, like first array 'data' element has 3 items, second has 1 item, 3rd and 4th are empty, and 5th has one item, I want to make a separate array like $temp_array['first_item_from_first_array,..., first_item_from_fifth_array, 'second_item_from_second_array,....,second_item_From_fifth_array]
input array is,
Array
(
[0] => Array
(
[meal_type] => bf
[label] => Breakfast
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
)
)
[1] => Array
(
[meal_type] => sn
[label] => Snacks
[calorie_limit] => 10
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
)
)
[2] => Array
(
[meal_type] => lu
[label] => Lunch
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
)
)
[3] => Array
(
[meal_type] => su
[label] => Supper
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
)
)
[4] => Array
(
[meal_type] => dn
[label] => Dinner
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
)
)
the output to be like this
Array
(
[0] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
[1] => Array
(
[0] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
[2] => Array
(
[0] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
)
You need to do it like below:-
$data_array= array_column($array,'data');
$count = 0;
foreach($data_array as $data){
$real_count = count($data);
if($real_count > $count){
$count = $real_count;
}
}
echo $count;
$final_array = [];
foreach($data_array as $data_arr){
for($i=0;$i< $count; $i++){
$final_array[$i][] = (count($data_arr[$i])>0)? $data_arr[$i]: array();
}
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/925383
Reference:- PHP: array_column - Manual
You can use array_column() function
Example :-
<?php
$array = [
[
"meal_type" => "bf",
"data" => [
"name" => "test",
"email" => "test#ymail.com"
]
],
[
"meal_type" => "bf",
"data" => []
]
];
echo "<pre>";
print_r(array_column($array, "data"));
?>
You can do this using foreach and array_push.
$data_item_array = [];
foreach ($array as $item) {
$data = $item['data'];
foreach ($data as $t) {
array_push($data_item_array, $t);
}
}
print_r($data_item_array);
Output will be :
Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[3] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)

Foreach to get data from array in wordpress php

Hi guys i have this array when i do print_r($p)
Array
(
[0] => Array
(
[product] => Array
(
[title] => test
[id] => 9
[created_at] => 2015-08-11 19:32:05
[isNew] =>
[type] => simple
[status] => publish
[price] => 10.00
[regular_price] => 10.00
[sale_price] => 6.00
[stock_quantity] => 19999985
[featured] => 1
[on_sale] =>
[description] =>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
)
[variations] =>
)
)
[1] => Array
(
[product] => Array
(
[title] => test222222
[id] => 97
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 1999974
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[2] => Array
(
[product] => Array
(
[title] => test222222
[id] => 98
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 199969
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[3] => Array
(
[product] => Array
(
[title] => test222222
[id] => 76
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variable
[status] => publish
[price] => 0.00
[regular_price] => 0.00
[sale_price] => 0.00
[stock_quantity] => 50000
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
)
)
[featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
)
null
i get this with this function
public function test(){
global $wpdb;
global $Pproduct;
global $woocommerce;
$productIds = 9_97_98_76;
$pId = explode("_", $productIds);
foreach($pId as $productID){
$product[] = $Pproduct->get_product($productID, $fields);
$p = $product;
}
print_r($p);
how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.
I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???
try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.
$product = Array();
foreach($pId as $productID){
array_push($product, $Pproduct->get_product($productID, $fields));
}
now try the following
foreach($product as $temp) {
echo $temp[variations];
}
Try this will get your title
$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}

How to parse elasticsearch response in php

I try to parse my elasticsearch response in php.
When i print my json resultat with a var_dump, i've got that:
Array (
[took] => 6
[timed_out] =>
[_shards] => Array (
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array (
[total] => 1
[max_score] => 0.44896343
[hits] => Array (
[0] => Array (
[_index] => car
[_type] => car
[_id] => DqE0c4ygRgC81o39DNmwhQ
[_score] => 0.44896343
[_source] => Array (
[currency] => EUR
[link] => myrurl
[reference] => A785454A
[brand] => mybrand
[model] => mymodel
[description] =>
[link_picture] => mylinkpicture
[price] => myprice
[km] => mykm
[start_years] =>
[active] => 1
[title] => mytitle
[ranking] => 22
[date_create] => 2014-05-26
)
)
)
)
)
But, when i try to list or to find a title by a foreach:
$myData = json_decode($response);
foreach ($myobj->hits->hits as $result) {
echo $result->_source->title;
}
that doesn't work, because i obtain this error:
Message: Invalid argument supplied for foreach()
I don't see why.
Thanks in advnce.
In foreach you are trying to use "$myobj".
But decoded json is in "$myData".

Store a variable from the array of object php

I have a php variabl $purchase_data which when I did
print_r('purchase_data');
I got the output as
Array
(
[downloads] => Array
(
[0] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
)
[fees] => Array
(
)
[subtotal] => 1
[discount] => 0
[tax] => 0
[price] => 1
[purchase_key] => a8d14e34ba425f9de6afe3ad4809587e
[user_email] => esh#test.com
[date] => 2014-05-11 20:07:22
[user_info] => Array
(
[id] => 1
[email] => eshtest.com
[first_name] => esh
[last_name] =>
[discount] => none
[address] =>
)
[post_data] => Array
(
[edd_email] => esh#test.com
[edd_first] => esh
[edd_last] =>
[edd_phone] => 919995871693
[edd-user-id] => 1
[edd_action] => purchase
[edd-gateway] => sample_gateway
)
[cart_details] => Array
(
[0] => Array
(
[name] => Shirt
[id] => 28
[item_number] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
[item_price] => 1
[quantity] => 1
[discount] => 0
[subtotal] => 1
[tax] => 0
[price] => 1
)
)
[gateway] => sample_gateway
[card_info] => Array
(
[card_name] =>
[card_number] =>
[card_cvc] =>
[card_exp_month] =>
[card_exp_year] =>
[card_address] =>
[card_address_2] =>
[card_city] =>
[card_state] =>
[card_country] =>
[card_zip] =>
)
)
How can i store the value to edd_phone [edd_phone] into variable $mobileNo ?
Sorry for ths kind of a question.But badly need help
If you want to assign the value of [edd_phone] to $mobileNo , use this :
$mobileNo = $purchase_data['post_data']['edd_phone'];
As a side note : Your array is very complexly nested. If I were you, I would avoid such complex arrays.

Categories