1

I want to be able to select a specific array within an array without using a number position. The reason I don't want to a use a number position because the position will not always be equal based on the user logged in.

For instance, with the following array, I want to select only that of which has the x value of YourPhone.

The array structure:

$salesArray = array(
    array(
        'key' => 'Basic Planners',
        'values' => array(
            array('x' => 'YourPhone', 'y' => 150),
            array('x' => 'Universe X3', 'y' => 300),
            array('x' => 'ePhone 74s', 'y' => 1500),
            array('x' => 'NextUs', 'y' => 50),
            array('x' => 'Humanoid', 'y' => 500)
        )
    ),
    array(
        'key' => 'No-Namers',
        'values' => array(
            array('x' => 'YourPhone', 'y' => 300),
            array('x' => 'Universe X3', 'y' => 250),
            array('x' => 'ePhone 74s', 'y' => 400),
            array('x' => 'NextUs', 'y' => 150),
            array('x' => 'Humanoid', 'y' => 900)
        )
    ),
    array(
        'key' => 'Feature Followers',
        'values' => array(
            array('x' => 'YourPhone', 'y' => 359),
            array('x' => 'Universe X3', 'y' => 900),
            array('x' => 'ePhone 74s', 'y' => 100),
            array('x' => 'NextUs', 'y' => 500),
            array('x' => 'Humanoid', 'y' => 250)
        )
    ),
    array(
        'key' => 'Hipsters & Elites',
        'values' => array(
            array('x' => 'YourPhone', 'y' => 200),
            array('x' => 'Universe X3', 'y' => 350),
            array('x' => 'ePhone 74s', 'y' => 50),
            array('x' => 'NextUs', 'y' => 800),
            array('x' => 'Humanoid', 'y' => 100)
        )
    )
  );

Right now I'm selecting the values as such:

<?php $currentTeam = 0; ?>

 <table>
   <?php foreach ($sales as $sale) { ?>
     <tr>
       <td><?php echo $sale['key']; ?></td>
       <td><?php echo $sale['values'][$currentTeam]['y']; ?></td>
     </tr>
   <?php } ?>
 </table>
1
  • 1
    Thanks and I appreciate the extra mile in the example. Commented Aug 5, 2014 at 4:21

2 Answers 2

1

Since you just want that particular key, just use an if condition to check, if it matches then print. Like this: Example

<?php $needle = 'YourPhone'; ?>
<table>
    <?php foreach ($salesArray as $sale) { ?>
        <?php
        $key = null;
        foreach($sale['values'] as $index => $info) {
            if($info['x'] == $needle) {
            // if(stripos($info['x'], $needle) !== false) {
                $key = $index;
                break;
            }
        }
        ?>
        <tr>
           <td><?php echo $sale['key']; ?></td>
           <td><?php echo $sale['values'][$key]['y']; ?></td>
        </tr>
    <?php } ?>
</table>
Sign up to request clarification or add additional context in comments.

Comments

1

This code block solves it:

MODIFIED

$currentTeam = "YourPhone"; ?>

 <table>
   <?php foreach ($salesArray as $sale) { ?>

     <tr>
       <td><?php echo $sale['key']; ?></td>
       <td><?php
            foreach($sale['values'] as $values){
                if($values['x'] == $currentTeam )
                    echo $values['y'];
            }

        ?>
        </td>
     </tr>
   <?php } ?>
 </table>

You need to use another foreach to get full control on the next array.

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.