How to pass and access the array variable using php - php

I have a array $param and while giving Print_r, the output as follows,
Array (
[pattern] =>
[status] => Array ( [0] => 0 [1] => 4 )
)
I have to pass the status value to one function like,
function value($action, $param){
// want to use the value here
}
how can i get the value here. please help

If you want to pass a multi-dimensional array as a parameter, simply pass the child array with the name of the parent (container array).
So if an array like
Array (
[pattern] =>
[status] => Array (
[0] => 0
[1] => 4
)
)
if you want both the elements of status to be passed into the function,pass the name of the array parent. (in this case, param)
function foo($x)
{
echo "<pre>"; // just to make reading easy ;)
print_r($x);
}
The function foo() displays the contents passed into the function which you can use to see what's being passed.
so things like foo($param['status'])
gives this :
Array( [0] => 0 [1]=> 4 )
and something like foo($param['status'][1])
gives this :
4

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;

Want to get array value

I have an array like below.
Here, I am getting array key like [_aDeviceTokens:protected] => Array.
$array= ApnsPHP_Message Object
(
[_bAutoAdjustLongPayload:protected] => 1
[_aDeviceTokens:protected] => Array
(
[0] => BD74940085E1579333E93B7D172CF82F5A3E0B17617D904107CD77573C42CEC9
)
[_sText:protected] => test
[_nBadge:protected] => 1
[_sSound:protected] => default
[_sCategory:protected] =>
[_bContentAvailable:protected] =>
[_aCustomProperties:protected] => Array
(
[channel_id] => 1xxxx8
[detail_id] => 1
)
[_nExpiryValue:protected] => 1500
[_mCustomIdentifier:protected] =>
)
As an array have object value so I am trying to get value of this key like,
$array->_aDeviceTokens:protected[0]
But this gives me an error.
So how can I achieve the value of these array keys?
It seems you are trying to access protected properties of an object that you are treating like an array.
Looking at the code here: https://github.com/immobiliare/ApnsPHP/blob/master/ApnsPHP/Message.php
There are publically accessible 'getters' for those attributes.
Extract of class ApnsPHP_Message:
public function getCustomIdentifier()
{
return $this->_mCustomIdentifier;
}
So instead of trying to access those properties as you have been, use the corresponding getter.
$custom_identifier = $message->getCustomIdentifier();
Convert object to array.
$array = json_decode(json_encode($array),true);
Now, you get value from $array like this
$array['_aDeviceTokens:protected'][0]

PHP Build An Array By Looping Back On Self Until All Records Are Found

I have a function in PHP (using Symfony) where I am building an array by passing a value into the array first, and then I am looping through every record but changing the key/value each time until I run out of values...
For example:
I pass this value into my getFieldKeys() function listed below:
array(
'fieldKey'=>'123'
);
Inside of this function, I first add this value to the fieldKey array and then see if there are any records that match my query...
If there are records then loop through each of those records and send the fieldKey the value back to the same function and add the value to the array...
This all works but my array look aweful. Need help cleaning up this code, not sure what to do...
public function getFieldKeys($array){
$em = $this->getEntityManager();
$fieldKeys[] = $array['fieldKey']; // add original value to fieldKey array...
// loop over any results and and pass key back into this same function and then do a look up on that key and repeat the process until finished...
$keys = $em->getRepository('AppBundle:FieldKeys')->findBy([
'fieldKey' => $array['fieldKey'],
]);
foreach($keys as $key) {
$fieldKeys[] = $this->getFieldKeys([
'fieldKey'=>$key->getFieldKey(),
]);
}
return $fieldKeys;
}
My final array looks like this - yikes!
Array
(
[0] => ccrs_date
[1] => Array
(
[0] => prelim_title_report_date
[1] => Array
(
[0] => additional_escrow_deposit_date
[1] => Array
(
[0] => earnest_money_date
[1] => Array
(
[0] => acceptance_date
[1] => Array
(
[0] => contract_date
[1] => Array
(
[0] =>
)
)
)
)
)
)
)
What I am hoping for is something more like this...
Array
(
[0] => ccrs_date
[1] => prelim_title_report_date
[2] => additional_escrow_deposit_date
[3] => earnest_money_date
[4] => acceptance_date
[5] => contract_date
[6] => prelim_title_report_date
)
Thanks!
You have written a recursive function to achieve your keys array - good idea. However your function returns an array and you are adding the resulting array recursivley to the current array which causes an array nesting one down per level.
This might be what you need as your second key is always an array but you are just looking for the key:
public function getFieldKeys($array){
$em = $this->getEntityManager();
$fieldKeys[] = $array['fieldKey']; // add original value to fieldKey array...
// loop over any results and and pass key back into this same function and then do a look up on that key and repeat the process until finished...
$keys = $em->getRepository('AppBundle:FieldKeys')->findBy([
'fieldKey' => $array['fieldKey'],
]);
foreach($keys as $key) {
$tmp = $this->getFieldKeys([
'fieldKey'=>$key->getFieldKey(),
]);
$fieldKeys[] = reset($tmp);
}
return $fieldKeys;
}

PHP: How to extract a property from this array

I have an array of values returned from Facebook - let's call it $array.
If I do print_r($array) - it looks like this:
Array
(
[code] => 200
[headers] => Array
(
[0] => Array
(
[name] => Some value
[value] => *
)
[1] => Array
(
[name] => Some value
[value] => Some value
)
[2] => Array
(
[name] => Some value
[value] => Some value
)
)
[body] => {"about":"Some more values.","can_post":true}
)
I need to extract the body part from this array.
I cannot refer to it by it's position, I'm looking for something like $array->body and receive the {....} string.
$array->body would work if the variable $array was an object
For arrays, just use:
$body = $array['body'];
(see: http://be2.php.net/manual/en/language.types.array.php)
If you want to access to your array via -> just do 1 more step:
$array = (object) $array;
And now, you can access to your body via:
$array->body;
Else without this step there is just one way:
$array['body'];
If you are more interested about converting arrays into objects, you can visit this question: How to convert an array to object in PHP?
Access array elements by using their name.
$array['body'];

Removing a value from a PHP Array

Using PHP I'm trying to remove an element from an array based on the value of the element.
For example with the following array:
Array
(
[671] => Array
(
[0] => 1
[1] => 100
[2] => 1000
)
[900] => Array
(
[0] => 15
[1] => 88
}
)
I'd like to be able to specify a value of on of the inner arrays to remove. For example if I specified 100 the resulting array would look like:
Array
(
[671] => Array
(
[0] => 1
[2] => 1000
)
[900] => Array
(
[0] => 15
[1] => 88
}
)
My first thought was to loop through the array using foreach and unset the "offending" value when I found it, but that doesn't seem to reference the original array, just the loop variables that were created.
Thanks.
foreach($array as $id => $data){
foreach($data as $index => $offending_val){
if($offending_val === 100){
unset($array[$id][$index]);
}
}
}
You can use:
array_walk($your_array, function(&$sub, $key, $remove_value) {
$sub = array_diff($sub, array($remove_value));
}, 100);
Couple of ideas:
You could try array_filter, passing in a callback function that returns false if the value is the one you want to unset. Using your example above:
$new_inner_array = array_filter($inner_array, $callback_that_returns_false_if_value_100)
If you want to do something more elaborate, you could explore the ArrayIterator class of the SPL, specifically the offsetUnset() method.

Categories