1

Please find below my array

Array ( 
[0] => Array ( 
    [id] => 0 
    [country] => GB 
    [note] => [email protected] 
    [cost] => 
    [company] => Aberdeen Debenhams 
    [address_1] => 
    [address_2] => Trinity Centre 
    [city] => 
    [state] => 
    [postcode] => AB11 6BD 
    [phone] => 01224 578553 
) 
[1] => Array ( 
    [id] => 1 
    [country] => GB 
    [note] => 
    [cost] => 
    [company] => Basildon Debenhams 
    [address_1] => 
    [address_2] => The Eastgate Centre 
    [city] => 
    [state] => 
    [postcode] => SS14 1HR 
    [phone] => 01268 244456 
)
)

The array is stored in the DB as a serialized array as a single WordPress Option.

I have a string:

$storename = 'Aberdeen Debenhams';

I have a function I use to search multidimensional arrays for a string.

if(search_array($storename, $option)) {
    echo 'true';
}

Now this rings true which is fine. The $storename is dynamic and comes from a previous function. What I need to do is be able to get the value of the [note] key within the same array so I can take that onto my next function.

UPDATE: In order for my next function to work I am using the following code to get the email address (key->note) from the same array as the $storename

//Get the email address of the storename                    
$key = array_search($storename, array_column($option,'company'));
echo $option[$key]['note'];

However, I am now getting the following.

Storename: Aberdeen Debenhams Email: [email protected]

Storename: Basildon Debenhams Email: [email protected]

It seems to be echoing the email address from the first array only.

2
  • echo $key = array_search($storename, $option); Commented Feb 15, 2017 at 19:44
  • Thanks @Amit but that didnt give me the note key, nor any other key. I need to get the value of the note key inside the same array as the $storename. Commented Feb 15, 2017 at 19:49

2 Answers 2

2

Use aray_search() along with array_column():-

echo $key = array_search($storename, array_column($option,'company'));

echo PHP_EOL;

echo $option[$key]['note'];

Output:-https://eval.in/737875 OR https://eval.in/737876 Or https://eval.in/737899

Note:-

this solution will work in the case of numeric-indexed array, not associative array.

Use if ($key !== false) { echo ... } suggested by @Kodos Johnson

Sign up to request clarification or add additional context in comments.

5 Comments

sorry I got your name wrong. I have made an update, your code is working but its only getting the first [note] from the arrays.
@AlexKnopp it's working for your update question too,check here:- eval.in/737899 ($storename need to be changed otherwise always get same value)
@AlexKnopp You should make sure that $key is not false before you use it. So do if ($key !== false) { echo ... } . This is because array_search returns false if the value was not found, and when you use false as the key for an array, it will assume 0 which is the first element of the array.
@Anant, thanks for the help. Adding the if statement fixed hte issues with returning the same email address.
@AlexKnopp glad to help you . :):)
1

You can solve it with an iteration, as below, also If you have an array of companies you can set foreach() that iterates companies in the big foreach().

foreach(array as arr){

   $aberdeen=new array();
   $basildon= new array();

   if(arr['company']=="Aberdeen Debenhams")
      $aberdeen = $arr;


    if(arr['company']=="Basildon Debenhams")
      $basildon = $arr;
 }

$basildon['note']; //(empty)
$aberden['note'];  //[email protected] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.