Asked  1 Year ago    Answers:  5   Viewed   12 times

I want to pass a function the index value of an array – e.g [‘client_name’] – a first level value works because I can do

        $index = client_name;

        function arraything ($index) { return $this->arraytolookat[$index]; }

The question is… how do I do this, if it’s a multi nested array?

I tried the eval statement and apparently it doesn’t evaluate brackets well … So I tried this.

        $index = “[0][‘client_name’]”;

        Eval(“$this->arraytolookat$index”);

But it just fails… winges about a unexpected [ - any Ideas?

EDIT: I do not know how many levels this function may be required to go into, therefore I cannot just append a set amount of brackets at the end. Its not as simple as it looks ^^

EDIT 2: Basically - I have written a form validation tool and one of the functions returns correct post data - I wanted a simple method that when you enter the name of the form element - it would literally return teh POST data back to the element e.g getFormData("client_name") - however when a form gets more complex, it can go into arrays, I need to prepare for the possibility of getFormData("['$i']client_name") or somthing along those lines, stuff happens to the postdata in that class so that function must be used. I just want that function to take in a string not an array.

 Answers

2

You can pass an array of indexes to this function below. So if you would like to get $some_array[0]['client_name']['third_level_index'] then you can do this:

function get_array_value(array $array, array $indexes)
{
    if (count($array) == 0 || count($indexes) == 0) {
        return false;
    }

    $index = array_shift($indexes);
    if(!array_key_exists($index, $array)){
        return false;
    }

    $value = $array[$index];
    if (count($indexes) == 0) {
        return $value;
    }

    if(!is_array($value)) {
        return false;
    }

    return get_array_value($value, $indexes);
}

$some_array = array(/* nested array */);

$indexes = array(0, 'client_name', 'third_level_index');

$value = get_array_value($some_array, $indexes);
Thursday, April 1, 2021
 
Ula
 
Ula
4

If I understand you correctly, you want to change the order of the elements but preserve the keys. You can do that with a small modification:

function shuffleThis($list) { 
    if (!is_array($list)) return $list; 

    $keys = array_keys($list); 
    shuffle($keys); 
    $random = array(); 
    foreach ($keys as $key) { 
        $random[$key] = $list[$key]; // CHANGE HERE that preserves the keys
    }
    return $random; 
}
Thursday, April 1, 2021
 
Tapha
 
2

The actual problem is you're not assigning the result of array_reverse to anything

// change this
array_reverse($respArr);

// to this
$respArr = array_reverse($respArr);
Saturday, May 29, 2021
 
1

Let's assume the array you posted is the content of a variable called $users. You can walk through it by doing

foreach ($users as $usr)
{
  $usr['@attributes']['id'];
  $usr['name'];
}

This way, you can go through every node without worrying about the indexes.

You may output the link on each foreach iteration in several ways. A complete example (which allows to use HTML without escaping every special character) could be:

<?php
foreach ($users as $usr)
{ ?>
  <a href="example.com?id=<?php echo $usr['@attributes']['id']; ?>&name=<?php echo $usr['name']; ?>"><?php echo $usr['name']; ?></a>

<?php } ?>

While it looks complex, with a lot of PHP opening and closing tags, it makes it easier on the markup with almost no performance penalty

Saturday, May 29, 2021
 
Semirix
 
4

I got it: the trick here is use the INDEX BY word.

Query class

import the Query class (no always optional):

use DoctrineORMQuery;

create the query:

$query = $this->data->em->createQuery('
    SELECT s 
    FROM modelsSetting s 
    INDEX BY s.arg //to set array custom key
    WHERE s.category = :category');
$query->setParameter('category', 'general');

set the hidration mode in order to work with read-only arrays

$settings = $query->getResult(Query::HYDRATE_ARRAY); 

Display the value:

echo $settings['desc']['value'];  // prints "bar"

QueryBuilder

With the QueryBuilder object you can set the index at the from statement:

$qb = $em->createQueryBuilder();
$qb->select('s');
$qb->from('modelsSettings', 's', 's.arg');  // here the magic
$result = $qb->getQuery()->getResult();

Then, you can access the object as:

$description = $result['desc'];
$value = $description->getValue();
Wednesday, July 28, 2021
 
Guesser
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :