Not able to access data inside a PHP array of object - php

I´m trying to access data inside a object create after a response(from a dafiti server) with no succsess.
Bellow my code:
***$response = Endpoints::order()->getOrder($orderId)->call($client);
$o = $response->getBody();
echo "<pre>";
var_dump($o);
echo "</pre>";
foreach($o as $value => $obj){
$orderId = $obj->Order->OrderID;// tried this way(not working)
$CustomerFirstName = $obj['Order']['CustomerFirstName'];// tried this way(not working)
}******
Bellow the var_dump:
array(1) {
["Orders"]=>
array(1) {
["Order"]=>
array(23) {
["OrderId"]=>
string(7) "8266761"
["CustomerFirstName"]=>
string(6) "sheila"
["CustomerLastName"]=>
string(14) "rocha domingos"
["OrderNumber"]=>
string(10) "4510948375"
["PaymentMethod"]=>
string(22) "braspag_cc_master_card"
["Currency"]=>
string(3) "BRL"
... to be continue.
How can I access the values inside this array?

Your array contains an array of orders inside of the 'Orders' field of the $o array. So you need to iterate over $o['Orders']. Then you'll be able to access your order details.
<?php
$o = [
'Orders' => [
'Order' => [
'OrderId' => '8266761',
'CustomerFirstName' => 'sheila',
'CustomerLastName' => 'rocha domingos',
'OrderNumber' => '4510948375',
'PaymentMethod' => 'braspag_cc_master_card',
'Currency' => 'BRL'
]
]
];
foreach ($o['Orders'] as $order) {
$orderId = $order['OrderId'];
$CustomerFirstName = $order['CustomerFirstName'];
var_dump($orderId); // 8266761
var_dump($CustomerFirstName); // sheila
}

Related

php get associative array in place normal array as result of function

i create function who return normal array :
function get_list_array () {
$list_object = get_list_objects();
foreach ( $list_object as $every_object) {
$list_array[] = array (
"wprm_$every_object->name" => array (
'name' => _x("$every_object->label", , 'test'),
'singular_name' => _x("$every_object->name", , 'test'),));
}
return $list_array ;
}
var_dump ($list_array);
array(2) {
[0]=> array(1) { ["object_1"]=> array(2) {
["name"]=> string(10)
"name_object1" ["singular_name"]=> string(15) "singular_name_object1" } }
[1]=> array(1) { ["object_2"]=> array(2) {
["name"]=> string(4)
"name_object2" ["singular_name"]=> string(10) "singular_name2" } } }
And i want the get in place just the associative array like this:
array ("object_1" => array (["name"]=> string(10) "name_object1"
["singular_name"]=> string(15) "singular_name_object1" } ,
"object_2" => array(2) {
["name"]=> string(4)
"name_object2" ["singular_name"]=> string(10) "singular_name2" } } }
any idea how i can modify my function in order the get the second output.
You're wrapping the array you actually want into another array by doing this:
$list_array[] = array(
"wprm_$every_object->name" => array(
Instead, you should simply assign the new array to $list_array directly:
$list_array["wprm_$every_object->name"] = array(
Also, please think about how you indent your code, because wow. Your function could look like this:
function get_list_array () {
$list_object = get_list_objects();
foreach ($list_object as $every_object) {
$list_array["wprm_$every_object->name"] = array(
'name' => _x("$every_object->label", , 'test'),
'singular_name' => _x("$every_object->name", , 'test'),
);
}
return $list_array;
}

Traverse an STD Class object inside an array

I've send an Array with data through FormData with Ajax.
I json encoded it first before sending it through and decoded it back at the PHP side.
My problem is this: "Trying to get property 'ID' of non-object" Or "Illegal string offset ID"
which is weird, since my array is like:
array(7) {
[0]=>
object(stdClass)#779 (2) {
["ID"]=>
string(1) "1"
["Value"]=>
string(19) "Onbeperkt helpdesk."
}
[1]=>
object(stdClass)#780 (2) {
["ID"]=>
string(1) "2"
["Value"]=>
string(43) "Een direct aanspreekpunt voor al uw vragen."
}
[2]=>
object(stdClass)#781 (2) {
["ID"]=>
string(1) "3"
["Value"]=>
string(20) "Een stabiel netwerk."
}
[3]=>
object(stdClass)#782 (2) {
["ID"]=>
string(1) "4"
["Value"]=>
string(43) "Uw belangrijke gegevens optimaal beveiligd."
}
}
The way I tried traversing the array:
$voordelen = json_decode($_POST['voordelen']);
echo var_dump($voordelen);
for ($i = 0; $i < count($voordelen); $i++) {
foreach ($voordelen[$i] as $key => $item) {
$voorArray = array(
"id" => $item->ID,
"item" => $item->Value,
"content_id" => $content->id // variable from code not shown here.
);
}
}
I also tried:
for ($i = 0; $i < count($voordelen); $i++) {
foreach ($voordelen[$i] as $item) {
$voorArray = array(
"id" => $item->ID,
"item" => $item->Value,
"content_id" => $content->id // Variable from code not shown here.
);
}
}
I also tried using $var['value'] instead of $var->value
But now I'm at a loss. Any help would be appreciated.
I think you are making an extra loop, did you try:
foreach ($voordelen as $item) {
$voorArray = array(
"id" => $item->ID,
"item" => $item->Value,
"content_id" => $content->id // Variable from code not shown here.
);
var_dump($voorArray);
}
What's wrong with just looping through it, since $voordelen is already an array:
$voordelen = json_decode($_POST['voordelen']);
foreach( $voordelen as $item )
{
$voorArray = array(
"id" => $item->ID,
"item" => $item->Value,
"content_id" => $content->id
);
// Do something with $voorArray here.
}

array_column not working for an array of objects?

This is my current array structure:
array(2) {
["Ground"] => array(4) {
[0] => object(Shipping\Model\Shipping)#337 (5) {
["shipping_id"] => NULL
["min_weight"] => string(4) "0.00"
["max_weight"] => string(4) "5.00"
["shipping_method"] => string(6) "Ground"
["shipping_rate"] => string(4) "8.00"
}
[1] => object(Shipping\Model\Shipping)#385 (5) {
["shipping_id"] => NULL
["min_weight"] => string(4) "6.00"
["max_weight"] => string(5) "10.00"
["shipping_method"] => string(6) "Ground"
["shipping_rate"] => string(5) "12.00"
}
}
["Expedited"] => array(4) {
[0] => object(Shipping\Model\Shipping)#388 (5) {
["shipping_id"] => NULL
["min_weight"] => string(4) "0.00"
["max_weight"] => string(4) "5.00"
["shipping_method"] => string(9) "Expedited"
["shipping_rate"] => string(5) "12.00"
}
}
}
Every time I run array_column for max_weight I get an empty array as a result. I'm supposed to use the returned array for max().
Is there a workaround for this?
v7.0.0: Added the ability for the input parameter to be an array of objects.
Source: https://secure.php.net/manual/en/function.array-column.php
The array_column is working correctly for me in PHP 7. Here is an example ...
<?php
$shippingMethods = array("Ground" => array());
$gMethod = new stdClass();
$gMethod->shipping_id = NULL;
$gMethod->min_weight = "0.00";
$gMethod->max_weight = "5.00";
$gMethod->shipping_method = "Ground";
$gMethod->shipping_rate = "8.00";
$shippingMethods["Ground"][] = $gMethod;
$gMethod = new stdClass();
$gMethod->shipping_id = NULL;
$gMethod->min_weight = "6.00";
$gMethod->max_weight = "10.00";
$gMethod->shipping_method = "Ground";
$gMethod->shipping_rate = "12.00";
$shippingMethods["Ground"][] = $gMethod;
$shippingMethod = "Ground";
$result = max(array_column(($shippingMethods[$shippingMethod]), "max_weight"));
echo $result;
?>

How can I flatten a Multidimensional array into a string?

Ive researched this but im coming up blank, Im generating an array of tests from a database like so:
$descriptions = array();
foreach ($tests as $value) {
array_push($descriptions, ['name' => $value['name']]);
}
I'm getting the desired out put but i'm getting a Multidimensional array of an array with '[64]' arrays inside '$descriptions', I need to convert this array so I get the following output:
'name' => $value1, 'name' => $value2, etc etc for all results,
I've tried implode, array_merge etc but the closest I've got is a flat array with only my last test: [name] => Zika can anyone point me in the right direction? cheers
You can't have duplicate array keys. But you can pass an array in like so:
<?php
$descriptions = array();
$tests = array(
'Zika', 'SARS', 'AIDS', 'Mad Cow Disease', 'Bird Flu', 'Zombie Infection',
);
foreach ($tests as $value) {
$descriptions[] = array('name' => $value);
}
var_dump($descriptions);
Which gives you :
array(6) { [0]=> array(1) { ["name"]=> string(4) "Zika" } [1]=> array(1) { ["name"]=> string(4) "SARS" } [2]=> array(1) { ["name"]=> string(4) "AIDS" } [3]=> array(1) { ["name"]=> string(15) "Mad Cow Disease" } [4]=> array(1) { ["name"]=> string(8) "Bird Flu" } [5]=> array(1) { ["name"]=> string(16) "Zombie Infection" } }
So you could foreach ($descriptions as $desc) and echo $desc['name']';
Have a look here: https://3v4l.org/pWSC6
If you just want a string, try this:
<?php
$descriptions = '';
$tests = array(
'Zika', 'SARS', 'AIDS', 'Mad Cow Disease', 'Bird Flu', 'Zombie Infection',
);
foreach ($tests as $value) {
$descriptions .= 'name => '.$value.', ';
}
$descriptions = substr($descriptions, 0, -2); // lose the last comma
echo $descriptions;
Which will output:
name => Zika, name => SARS, name => AIDS, name => Mad Cow Disease, name => Bird Flu, name => Zombie Infection
See it here https://3v4l.org/OFGF4

Reading PHP array using key value

I have an array that I'm creating inside my PHP script, the var_dump function gives me this value :var_dump($arrayOfValues);
array(3) {
[0]=> array(2) {
[0]=> string(12) "BusinessName"
[1]=> string(13) "ITCompany"
}
[1]=> array(2) {
[0]=> string(7) "Address"
[1]=> string(58) "29 xxxxxx,Canada"
}
[2]=> array(2) {
[0]=> string(20) "PrimaryBusinessPhone"
[1]=> string(14) "(444) 111-1111"
}
[3]=> array(2) {
[0]=> string(13) "BusinessEmail"
[1]=> string(24) "xx#example.com"
}
}
I would like to access to the value of the "BusinessName" using key and not index so if I put : echo $arrayOfValue[0][1]; it gives me the BusinessName that is ITCompany but if I put
: echo $arrayOfValue['BusinessName'][1]; it gives nothing so how can I fix that please?
The array is initialized $arrayOfValue = array(); and then populated dynamically inside a for loop like that
$arrayOfValue[] = array($Label, $Value);
your array has this kind of data
$arrayOfPostsValue[] = array("BusinessName","ITCompany");
$arrayOfPostsValue[] = array("Address","29 xxxxxx,Canada");
$arrayOfPostsValue[] = array("PrimaryBusinessPhone","(444) 111-1111");
$arrayOfPostsValue[] = array("BusinessEmail","xx#example.com");
there is no array key in data So, you have to recreate your desire array
$newArrayOfPostsValue = array();
foreach ( $arrayOfPostsValue as $value ){
$newArrayOfPostsValue[$value[0]] = $value[1];
}
print_r($newArrayOfPostsValue);
and here is output
Array
(
[BusinessName] => ITCompany
[Address] => 29 xxxxxx,Canada
[PrimaryBusinessPhone] => (444) 111-1111
[BusinessEmail] => xx#example.com
)
As mentioned in the comment, change the structure of the array, it will be much easier to handle
$my_array = array(
array('Business Name' => 'It Company'),
array('Address' => 'My address')
);
Looking at the content of your array, I will restructure it as
$my_improved_array = array(
'BusinessName' => 'It Company',
'Address' => 'My Address',
);
This is how you can access,
echo $my_array[0]['BusinessName'] //prints 'It Company'
echo $my_array[1]['Address'] //prints 'My Address'
echo $my_improved_array['BusinessName'] // prints 'It Company'
Try like this and save array as key value pairs and then access the key:
foreach($arrayOfValues as $a)
$arr[$a[0]] = $a[1];
echo $arr['BusinessName'];
While creating that array build it with index as BusinessName
array (
0 => array(
'BusinessName'=> "ITCompany"
)
1 => array(1) (
'Address'=> "29 xxxxxx,Canada"
)
)
etc..
PHP Array example
$array = array();
$array['BusinessName'] = 'ITCompany';
$array['Address'] = '29 xxxxxx,Canada';
And so on... Now you can echo values with
echo $array['BusinessName'];
Also this works
$array = array('BusinessName' => 'ITCompany', 'Address' => '29 xxxxxx,Canada');
First of all, how are you building this array ? Maybe you can directly make a workable array.
Anyway, you can do something like this :
$oldArray = Array(
Array("BusinessName", "ITCompany"),
Array("Address", "29 xxxxxx,Canada"),
Array("PrimaryBusinessPhone", "(444) 111-1111"),
Array("BusinessEmail", "xx#example.com")
);
$newArray = Array();
foreach($oldArray as value) {
$newValue[$value[0]] = $value[1];
}
/* Now, it's equal to
$newArray = Array(
"BusinessName" => "ITCompany",
"Address" => "29 xxxxxx,Canada",
"PrimaryBusinessPhone" => "(444) 111-1111",
"BusinessEmail" => "xx#example.com"
);
*/

Categories