How can i get the values of the array bellow?
Array
(
[list_items] => Array
(
[0] => Array
(
[productName] => BUGGY INFANTIL PRO 125CC
[productId] => 1
[productColor] => Azul
[productQuantity] => 2
[productIMG] => http://localhost/kasi_src/img/vehicules/buggygasolina/BUGGY-INFANTIL-125CC-AZUL.png
[productURL] => http://localhost/kasi_src/125CCInfantil.html
[productPrice] => 1000
)
[1] => Array
(
[productName] => PATINETE 24V
[productId] => 2
[productColor] => Azul
[productQuantity] => 2
[productIMG] => http://localhost/kasi_src/img/vehicules/patinetes/24.png
[productURL] => http://localhost/kasi_src/patinete-24v.html
[productPrice] => 240
)
)
)
I tried a simple foreach loop
foreach($items as $key => $value){
echo $value . "\n";
}
but i ended up getting this error : Notice: Array to string conversion in .....
Any ideas ?
You cannot echo a array.
foreach($array["list_items"] as $list_item){
echo $list_item["productName"];
}
Use var_export($obj,true) if you want to get a textual representation of the full content of each array associated to a key in an associative array as in the example, or simply access the keys of each subarray to retrieve individual values as productName.
<?php
$list_items = array(
0 => array("productName" => "Marshmallows"),
1 => array("productName" => "Mikado"),
);
foreach($list_items as $key=>$obj){
echo var_export($obj,true)."<br/>";
// Or access each array key, for example: echo $obj['productName'];
}
?>
Output:
array ( 'productName' => 'Marshmallows', )
array ( 'productName' => 'Mikado', )
Related
I just have no idea how to sum and echo values in multidimensional array. This is my array with info about order.
I need to sum RESULT_VALUE and echo one united DESCR list. I have tried lots of things but nothing, never worked with that kind of arrays levels. This is an array with two different products with applied discounts.
Array
(
[0] => Array
(
[ORDER_ID] => 35
[DISCOUNT_ID] => 18
[ORDER_COUPON_ID] => 0
[COUPON_ID] =>
[RESULT] => Array
(
[BASKET] => Array
(
[617] => Array
(
[RULE_ID] => 248
[APPLY] => Y
[RULE_DESCR_ID] => 248
[ACTION_BLOCK_LIST] => Array
(
[0] => 0
)
[DESCR_DATA] => Array
(
[0] => Array
(
[TYPE] => 2
[VALUE] => 10
[VALUE_TYPE] => P
[VALUE_ACTION] => D
[RESULT_VALUE] => 229.905
[RESULT_UNIT] => RUB
)
)
[DESCR] => Array
(
[0] => discount 10% (229.91 rub.)
)
[DESCR_ID] => 248
[BASKET_ID] => 617
[MODULE] => catalog
[PRODUCT_ID] => 10109
)
[618] => Array
(
[RULE_ID] => 249
[APPLY] => Y
[RULE_DESCR_ID] => 249
[ACTION_BLOCK_LIST] => Array
(
[0] => 0
)
[DESCR_DATA] => Array
(
[0] => Array
(
[TYPE] => 2
[VALUE] => 10
[VALUE_TYPE] => P
[VALUE_ACTION] => D
[RESULT_VALUE] => 70.796
[RESULT_UNIT] => RUB
)
)
[DESCR] => Array
(
[0] => discount 10% (70.80 rub.)
)
[DESCR_ID] => 249
[BASKET_ID] => 618
[MODULE] => catalog
[PRODUCT_ID] => 10054
)
)
)
[ACTION_BLOCK_LIST] => 1
)
You can use array_walk_recursive to get result. Code below sum all values of key $key wherever they are in the array
$key = 'RESULT_VALUE';
$sum = 0;
array_walk_recursive ($array,
function($v, $k, $key) use (&$sum) {
if($k === $key) { $sum += $v;}},
$key);
echo $sum;
Echo array_sum(array_column($array, 'RESULT_VALUE'));
This solution work ?
You can use array_sum and array_column.
Array_column grabs all values from one column in the array, and array_sum... Sums it...
echo array_sum(array_column($arr, "RESULT_VALUE"));
I tried to parse a string array using the code below but the required data never printed! could any one tell me how to fix it ?Thanks
$data Array structure :
Array
(
[js] => Array
(
[total_items] => 20
[max_page_items] => 2
[selected_item] => 0
[cur_page] => 0
[data] => Array
(
[0] => Array
(
[tmp] => 1
[name] => mango
[abc] => abcd4 http://mysite/items/1234
[number] => 1123
[itemCategory_title] => fruits
[logo] => 2123.png
[itemCategory_id] => 90
)
[1] => Array
(
[tmp] => 0
[name] => cherry
[abc] => abcd4 http://mysite/items/1235
[number] => 1124
[itemCategory_title] => fruits
[logo] => 2124.png
[itemCategory_id] =>
)
)
)
[text] => no error
)
php code:
<?
$code2 = stripslashes($_POST['outputtext']);
$data = json_decode($code2);
foreach( $data as $item ) {
echo $item['tmp'];
echo $item['name'];
echo $item['abc'];
echo $item['number'];
echo $item['itemCategory_title'];
echo $item['log'];
echo $item['itemCategory_id'];
}
?>
It should be:
foreach ($data['js']['data'] AS $item)
because the array is nested several levels down in $data.
Note that you need to call json_decode($code2, true) to get an associative array like that. By default, it returns an object, not an array, so you would do:
foreach ($data->js->data as $item) {
echo $item->tmp;
echo $item->name;
...
}
I'm not sure how I would loop through this array to get a value from the 3rd array ([0] => Array). How would I go about getting the value of leaguePoints?
Array
(
[4605] => Array
(
[0] => Array
(
[name] => Hecarim's Duelists
[tier] => DIAMOND
[queue] => RANKED_SOLO_5x5
[entries] => Array
(
[0] => Array
(
[playerOrTeamId] => 4605
[playerOrTeamName] => External
[division] => I
[leaguePoints] => 17
[wins] => 223
[isHotStreak] =>
[isVeteran] => 1
[isFreshBlood] =>
[isInactive] =>
)
)
)
)
)
try for getting all values of array
foreach($arr[4605][0]['entries'][0] as $v) {
echo $v;
}
for getting in single statement try
echo $arr[4605][0]['entries'][0]['leaguePoints'];
This is 5 nested arrays. To get leaguePoints, you will need to use something similar to
echo $arr[4605][0]['entries'][0]['leaguePoints'];
I have one array called $stocknumb
$stocknumb= Array ( [0] => 102 [1] => 103 [2] => 104 )
Another array called $price it contains both $stocknumb as well as price
$price=Array ( ['102'] => Array ( [0] => 2000 ) ['103'] => Array ( [0] => 3 ) ['104'] => Array ( [0] => 4000 ) )
Now i would like to get prices for only stock numbers which are in $stocknumb array. Both price and stock number are in $price array.
How to get it in PHP.
See array_column function (php >= 5.5) :
$price= array(
102 => array(0 => 2000),
103 => array(0 => 3),
104 => array(0 => 4000)
);
$array_price = array_column ($price, 0);
print_r($array_price);
/*Array
(
[0] => 2000
[1] => 3
[2] => 4000
)
*/
If you don't use php 5.5, see :
https://github.com/ramsey/array_column/blob/master/src/array_column.php
If you want final data as array then with conventional way of looping could be done as
$stocknumb= Array ( 0 => 102, 1 => 103, 2 => 104 ) ;
$price=Array ( '102' => Array ( 0 => 2000 ), '103' => Array ( 0 => 3 ), '104' => Array ( 0 => 4000 ) );
$final_array = array() ;
foreach($stocknumb as $key=>$val){
if(array_key_exists($val,$price)){
$final_array[$val] = $price[$val][0];
}
}
print_r($final_array);
Output
Array ( [102] => 2000 [103] => 3 [104] => 4000 )
You can use array intersect function
$stocknumb= array ( 102, 103 );
// Make the key same as the value.
$stocknumb = array_combine($stocknumb, $stocknumb);
$price= array (102 => array (2000), 103 => array (3), 104 => array (4000 ));
$filtered = array_intersect_key($price, $stocknumb);
A single line of code with array_flip() and array_key_intersect() will suffice
$new_arr=array_intersect_key($price, array_flip($stocknumb));
OUTPUT :
Array
(
[102] => Array
(
[0] => 2000
)
[103] => Array
(
[0] => 3
)
[104] => Array
(
[0] => 4000
)
)
<?php
foreach($price as $key => $value) {
if(in_array($key, $stocknumb)) {
echo $value[0];
}
}
DEMO
I have an array in php like this :
Array
(
[0] => Array
(
[915] => 1
[1295] => 1
[1090] => 1
[1315] => 0.93759357774
[128] => 0.93759357774
[88] => 0.731522789561
[1297] => 0.731522789561
[1269] => 0.525492880722
[1298] => 0.525492880722
[121] => 0.519133966069
)
[1] => Array
(
[585] => 1
[1145] => 1
[1209] => 1
[375] => 1
[1144] => 1
[913] => 1
[1130] => 0.996351158355
[215] => 0.937096401456
[1296] => 0.879373313559
[30] => 0.866473953643
[780] => 0.866473953643
[1305] => 0.866473953643
[1293] => 0.866473953643
)
)
How do I get the 1st-5th rows of sub-array for each array, like this :
Result :
Array
(
[0] => Array
(
[915] => 1
[1295] => 1
[1090] => 1
[1315] => 0.93759357774
[128] => 0.93759357774
)
[1] => Array
(
[585] => 1
[1145] => 1
[1209] => 1
[375] => 1
[1144] => 1
)
)
$multid_array = array(/* Your Multidimensional array from above*/);
$sliced_array = array(); //setup the array you want with the sliced values.
//loop though each sub array and slice off the first 5 to a new multidimensional array
foreach ($multid_array as $sub_array) {
$sliced_array[] = array_slice($sub_array, 0, 5);
}
The $sliced_array will then contain the output you wanted.
Iterate over the array.
Read the value by reference.
Delete key-values from offset 5 till
the end. You need not collect the return value because we are using the reference to the original array.
.
foreach($mainArray as $key => &$value) {
array_splice($value,5);
}
Working ideone link
You might want to look into the php function array_splice.
http://no.php.net/manual/en/function.array-slice.php