I would like to search value of order using array_search function i have tried following ways but it does not work.
printed array display output
PostDaataArray
(
[order_id] => 5464
)
$currentKey=array_search($orderId,$postedData);
also tried $currentKey=array_search($orderId,array_column($postedData, 'order_id'));
But when i tried to search array using array_search function it does not work also not showing error as well.
If previously data in JSON format then decode it:
$postedData = json_decode($postedData,true);
You can use in_array():
if(in_array($orderId,array_column($postedData, 'order_id'))) //check value is in array
{
$key = array_search($orderId,array_column($postedData, 'order_id')); //return index or key of array
}
else
{
//order id not in array
}
try this:
$PostDaataArray=array("order_id"=>"5464");
foreach($PostDaataArray as $key=>$order_id){
if($order_id=="5464"){
// match found.
}
}
Related
$data = Array
(
[68315163] => Donnie1
[68328887] => Donnie1
[68353339] => Donnie1
)
I want to get the all the keys for Donnie1 value it is showing only the first one
$datum = array_search('Donnie1', $data);
print_r($datum);
Where am I going wrong?
array_search() does not search array keys. It only searches array values.
Getting this value is basic PHP:
$datum = $data['68315163'];
array_search('68315163', $data) doesn't return anything useful because the value you're searching for is not in the array.
This function searches through the values, and returns the key at the found value. Please see the docs.
Array
(
[68315163] => Donnie
[68328887] => Donnie1
[68353339] => Donnie2
)
$datum = array_search('Donnie1', $data);
echo $datum;// return only value of given key: 68328887
You are passing wrong parameters to array_search(). you need to pass value of array then this function will return matching key;
array_search() does not return array. It only returns the first key.
array_keys() would be the correct function for this use. It returns an array of all keys with the given value.
$datum = array_keys($data, "Donnie1");
How would I setup an associative array to reference specific values at different sections of a page. My function:
<?php
function park_data($park_page_id) {
$data = array();
if($park_page_id){
$data = mysql_fetch_assoc(mysql_query("SELECT * FROM `park_profile` WHERE `park_id` = $park_page_id"));
return $data;
}
}
?>
My print_r:
<?php
print_r (park_data(1));
?>
Produces the following associative array:
Array ( [park_id] => 1 [park_name] => Kenai Fjords [park_address] => 1212 4th Avenue [park_city] => Seward [park_state] => Alaska [park_zip] => 99664)
How would I print just the [park_name] value from this array?
From the docs:
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
// on PHP 5.4
print_r(park_data(1)['park_name']);
// earlier versions
$tmp = park_data(1);
print_r($tmp['park_name']);
$park=park_data(1);
echo $park['park_name'];
To output custom formatted text in general, and in this case to output only a single array's key value, use echo, because print_r() called on an array displays the whole array's structure and content, and that's not what you want:
<?php
// code
$park_data=park_data(1);
echo $park_data["park_name"];
// code
?>
I have an array which gives me correct results when I print it, for example:
[0] => info#mail.com,
[1] => 0909,
[2] => info#mail.com22,
[3] => 0909
Now, when I want to check if info#mail.com is in the array it gives me an error that the value doesnt exist in this array, but when I try for example info#mail.com22 it gives the correct result.
This is a little part of the code:
$user is the word I want to search, $arrayname is the array.
if (array_search(strtolower($user),array_map('strtolower',$arrayname))){
//value exist
}
else{
//value does not exist
}
Now info#mail.com doesn't exist it says, while info#mail.com22 does exist.
Who has any idea?
array_search returns the index of the value that is found. When you search for the first item it returns 0. which is also means false. change your code so that it reads
if (false !== array_search(strtolower($user),array_map('strtolower',$arrayname))){
an alternative method would be to use in_array
if(in_array(strtolower($user),array_map('strtolower',$arrayname))){
I simply use is_numeric
if (is_numeric(array_search(strtolower($user), $arrayname)) {
/* do something */
}
I have an array $aMethods whose print_r output is this:
Array
(
[0] => Array
(
[pattern] =>
[return_media] => 1
[return_name] =>
)
)
I'm trying to access 'return_media' with this code:
$iReturnMedia = $aMethods[0]->return_media;
echo $iReturnMedia;
Also, when I tried this:
$iReturnMedia = $aMethods[0]['return_media'];
I get an error stating: Cannot use string offset as an array in...
But it's not working, $iReturnMedia comes back as blank. Could someone tell me what I'm doing wrong here?
EDIT: $aMethods is set in a foreach loop as such:
foreach ($aMethodList as $sMethodGroup => $aMethods) { //insert code from above }
You need to use:
$iReturnMedia = $aMethods[0]['return_media'];
The operation -> is for accessing object properties. Since you're just dealing with nested arrays, you need to index them with [].
Access the array value by key.
$iReturnMedia = $aMethods[0]['return_media'];
echo $iReturnMedia;
Your accessing it as if it was an object in an array, you do it like:
$iReturnMedia = $aMethods[0]['return_media'];
echo $iReturnMedia;
Try this,
$iReturnMedia = $aMethodList[$sMethodGroup][0]['return_media'];
echo $iReturnMedia;
Try to var_dump($aMethods) . It will be give exactly idea of that array...
find below the code to access the array values -
foreach ($aMethodList as $sMethodGroup => $aMethods) {
echo $aMethods[0]['return_media'];
}
I am having a bit of difficulty with this. I want to allow a user to check if a username is available through an AJAX request. The AJAX request calls my php and PHP returns true if the username is not available or false if available.
I wanted to merge the username into the array (if found) and then use in_array to locate a match. It isn't working this way however.
$res = // database returns any username that matches - (not an array)
$banned = // database returns an assoc array of banned names
array_push($banned, strtolower($res['user']));
if(!in_array(strtolower($requested), $banned)){
echo 'available';
} else {
echo 'not available';
}
Here is a sample array from the banned variable:
Array
(
[0] => bad1
[1] => bad2
[3] =>
)
The 3rd key is null because it wasn't found in the $res variable.
Is there a better way to do this? I also need to convert the values in the array to lowercase as well.
For readability, I reckon this would look better
if (isset($res['user'])) { // is this key set for this array?
$banned[] = strtolower($res['user']); // append the strtolower`d version
}