How can I access ID array of its value - php

I am analyzing someone's else code where I found during debugging the type and values of a class variable during run time:
echo print_r($this->_out);
Array
(
[id] => -1
[fieldErrors] => Array
(
)
[error] =>
[data] => Array
(
)
[row] => Array
(
[DT_RowId] => row_177
[id] => 177
[last_name] => sdfdsf
[first_name] => dsf
[homeaddr] => sdfdsfsdfdsfdsfdsfdsf
[email] => s#jj.com
[officeaddr] => wwwwwwwwwwwwwwwwwwwwwwww
[mobile] => 11111111
[age] => 11
[chargeamt] => 11
[start_date] => 11/11/2011
)
)
1{"row":{"DT_RowId":"row_177","id":"177","last_name":"sdfdsf","first_name":"dsf","homeaddr":"sdfdsfsdfdsfdsfdsfdsf","email":"s#jj.com","officeaddr":"wwwwwwwwwwwwwwwwwwwwwwww","mobile":"11111111","age":"11","chargeamt":"11","start_date":"11\/11\/2011"}}
I am a newbie in PHP and would like to know how I can access [id] => 177 value i.e. value 177.
I tried out many ways
$this->_out['row']['id'][0]
It gave me below result:
1{"row":{"DT_RowId":"row_177","id":"177","last_name":"sssss","first_name":"ss","homeaddr":"sssssssssssssssssssss","email":"ss#ww.com","officeaddr":"sssssssssssssssssssssssssssss","mobile":"11111111","age":"11","chargeamt":"11","start_date":"01\/01\/2001"}}
while
I tried out many ways
$this->_out['row']['id']
It gave me below result:
177{"row":{"DT_RowId":"row_177","id":"177","last_name":"sssss","first_name":"ss","homeaddr":"sssssssssssssssssssss","email":"ss#ww.com","officeaddr":"sssssssssssssssssssssssssssss","mobile":"11111111","age":"11","chargeamt":"11","start_date":"01\/01\/2001"}}
and others but its just not giving me the expected.
How can I access the value as desired?

You are doing it right. $this->_out['row']['id'] will return desired result (check why you get also JSON string that is not part of print_t($this->_out).
This will return result 177:
$this->_out['row']['id'];
And since in PHP you can access string characters as array, this will returns first character in string (that is 1):
$this->_out['row']['id'][0];
And this will throw error as there is no such index (string length is 3, so last index is 2):
$this->_out['row']['id'][5];

The print_r result is an array with more arrays on it. So first of all you must find the index of the main array which index represents the sub-array with the value you are looking for. And then you must use this index to access the sub array values.

Related

Output the imtId value

Please tell me how to output the imtId value, the array is not complete, I will not output further, but the meaning should be clear.Thank you in advance
Array
(
[id] => mavrin-wildberries-1635334576193516728
[jsonrpc] => 2.0
[result] => stdClass Object
(
[cards] => Array
(
[0] => stdClass Object
(
[id] => d3c33a3f-f5b3-5647-8e7a-ad50d27d4417
[imtId] => 30306963
[userId] => 0
[supplierId] => e9b901b9-b663-5648-97b8-6313d0e245ba
[imtSupplierId] => 0
I tried:
echo ['result']['cards'][0]['nmId']
A few things:
You need to echo an actual variable, not just a series of indexes.
The item inside "result" is an object, not an array
So is the item within the "0" index.
There is no such index as "nmld" - you said you wanted "imtId" instead, so I don't know why you didn't use that?
Therefore, if this data is contained in a variable called $arr then something like
echo $arr["result"]->cards[0]->imtId;

how to create the multiple values with same key in array in php [duplicate]

This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed last month.
I have an array with multiple key.
Array 1
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Output
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
Expected Output
$age = array("value"=>"35", "value"=>"37", "value"=>"43");
Array ( [value] => 35 [value] => 37 [value] => 43 )
You can't
Indeed, array keys must be unique. Otherwise, what should the program output when you try to access value ?
But ...
If you need to store a list of value for one key, you can use array of arrays.
$array = array("value" => array());
array_push($array["value"], 35, 40, 53);
print_r($array)
And the output will be:
Array
(
[value] => Array
(
[0] => 35
[1] => 40
[2] => 53
)
)
Only way is to turn this array into 2D array:
$age = array(
array("value" => "35"),
array("value" => "37"),
array("value" => "43")
);
-- Output --
Array
(
[0] => Array
(
[value] => 35
)
[1] => Array
(
[value] => 37
)
[2] => Array
(
[value] => 43
)
)
-- Usage --
$age[0]['value'];
$age[1]['value'];
$age[2]['value'];
But it completely depends if $age array is in our control and can be changed.
You can't
Array keys must be unique.
http://php.net/manual/en/language.types.array.php
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Yes, array key should be unique. So, whatever you are asking it's not possible. Can you tell us what's requirements? So that, people can suggest any alternate solution.
This cannot be possible with native php arrays. What you need is a multimap, and you can find several implementations of it on github.
For example: link
EDIT: the link above is an interface. you need to include also link2

Accessing Array Data in Object

I'm trying to access a piece of data in an array of arrays that (I believe) is in an object (this may not be the right term though).
When I do print_r on this: $order_total_modules->process() I get...
Array (
[0] => Array (
[code] => ot_subtotal
[title] => Sub-Total:
[text] => $49.99
[value] => 49.99
[sort_order] => 1
)
[1] => Array (
[code] => ot_total
[title] => Total:
[text] => $0.00
[value] => 0
[sort_order] => 12
)
)
If I run echo $order_total_modules->process()[1][3];, I should get "0", because that is the 3rd element of the 2nd array... right? Yet, I get an error.
Can anyone help with this?
Even though it is the third element counting from 0, the index is not 3 it is an associative array with the index value:
Available in PHP >=5.4.0:
echo $order_total_modules->process()[1]['value'];
Or PHP < 5.4.0:
$result = $order_total_modules->process();
echo $result[1]['value'];
You cannot access an associative array via an integer index(unless the index is an actial integer).
So in this case use :
[1]['code'] to access what woulde be [1][0] with a 'normal' array.
Try putting it in a var first:
$ar = $order_total_modules->process();
echo $ar[1]['value'];
The second level array is an assoc, which means that the key is not numeric, which means that you need to call the name of the key, hence the 'value'.

array_unique showing error Array to string conversion

For getting the unique values i am using unique values. Below is the code i am using
array_unique($results_external);
aasort($results_external,"created_on");
$returns_buy_external[]=array_reverse($results_external, true);
If i use the code like this, below is the error i am getting
A PHP Error was encountered Severity: Notice
Message: Array to string conversion
Filename: models/product_model.php
Line Number: 3550
3550 line is array_unique($results_external);
Can anyone help me, why it is getting error like this and how to solve it?
results_external sample format coming is below
Array
(
[0] => Array
(
[id] => 144
[name] => test
[slug] => test
[images] => {"9eebd0f69772dd3bdf8c787864437c85":{"filename":"9eebd0f69772dd3bdf8c787864437c85.png","alt":"TRESemme Smooth and Shine","caption":""}}
[track_stock] => 1
[seo_title] => ttt
[qty] => 0
[product_type] => 0
[price] => 0.00
[saleprice] => 0.00
[external_links] => http://test.com
[external_price] => 285.00
[external_saleprice] => 285.00
[created_on] => 2013-11-08 15:03:24
)
)
As per the docs, array_unique compares elements as strings by default. This means your 2D array is being converted to an array of strings (all being "Array" and generating the array-to-string Notice) or which only one can be returned as unique.
Use the SORT_REGULAR flag to compare the elements as they are, but be aware that arrays are only considered equal if they have the same key-value pairs.
Example:
print_r(array_unique($array, SORT_REGULAR));

Sorting Multidimensional Array

Thought I will point out first that I have looked around on Stackoverflow and the internet already and although they are plenty of examples none of them are explained into such a way for me to understand how to convert the code to work with my array structure.
I believe I need to use one of the functions uksort or uasort but unsure on this as well.
My array looks like the following.
Array
(
[0] => Array
(
[Result] => Array
(
[id] => 260
[event_id] => 72
[year] => 2010
[york_score] => 27
[york_points] => 0.0
[lancs_score] => 51
[lancs_points] => 4.0
[winner] => L
[confirmed] => Y
[updated] => 2010-05-01 16:10:03
)
[Event] => Array
(
[id] => 72
[sport_id] => 25
[event_title] => 1st Team
[Sport] => Array
(
[id] => 25
[sport_title] => Netball
)
)
)
And where its [0] means it continues on.
I need to sort all of the arrays [0,1,2,3,...] by the sport_title key found in [Event][Sport]
Does anyone know how to create a sorting function to do this?
Some explanation of how the function is working would also be helpful if I later need to adapter/alter the code to work else where on my site.
Where $array is the name of the array which holds the array you posted in your question.
function sort_multi($item_1, $item_2)
{
// strcmp looks at two strings and, based off the characters' and their order,
// determines which one is numerically greater. When this function returns a
// negative, for example, it means the first item it's comparing is less that the
// second item (ef and eg, for example). The usort function then rearranges
// the array based off these comparisons.
return strcmp($item_1['Event']['Sport']['sport_title'], $item_2['Event']['Sport']['sport_title']);
}
usort($array, 'sort_multi');

Categories