Viewed   73 times

If I have:

$array = array( 'one' =>'value', 'two' => 'value2' );

how do I get the string one back from $array[1] ?

 Answers

4

You don't. Your array doesn't have a key [1]. You could:

  • Make a new array, which contains the keys:

    $newArray = array_keys($array);
    echo $newArray[0];
    

    But the value "one" is at $newArray[0], not [1].
    A shortcut would be:

    echo current(array_keys($array));
    
  • Get the first key of the array:

     reset($array);
     echo key($array);
    
  • Get the key corresponding to the value "value":

    echo array_search('value', $array);
    

This all depends on what it is exactly you want to do. The fact is, [1] doesn't correspond to "one" any which way you turn it.

Tuesday, November 22, 2022
5

Without making use of loops.

<?php
    $myArrayInit = [1 => 'red', 30 => 'orange', 25 => 'velvet', 45 => 'pink']; //<-- Your actual array
    $offsetKey = 25; //<--- The offset you need to grab

    //Lets do the code....
    $n = array_keys($myArrayInit); //<---- Grab all the keys of your actual array and put in another array
    $count = array_search($offsetKey, $n); //<--- Returns the position of the offset from this array using search
    $new_arr = array_slice($myArrayInit, 0, $count + 1, true);//<--- Slice it with the 0 index as start and position+1 as the length parameter.
    print_r($new_arr);

Output :

Array
(
    [1] => red
    [30] => orange
    [25] => velvet
)
Friday, November 18, 2022
 
2

Why not just modify the loop you already use to display the data?

echo "<table>";
foreach($req_data as $key=>$row) {
    echo "<tr>";
    foreach($row as $key2=>$row2){
        echo "<td>" . $row2 . "</td>";
    }
    echo "</tr>";
}
echo "</table>";
Friday, September 23, 2022
 
anchor
 
4

You can use :

$array = [
        "ID",
        "UUID",
        "pushNotifications.sent",
        "campaigns.boundDate",
        "campaigns.endDate",
        "campaigns.pushMessages.sentDate",
        "pushNotifications.tapped"
];

// Build Data
$data = array();
foreach($array as $v) {
    setValue($data, $v, 1);
}

// Get Value
echo getValue($data, "campaigns.pushMessages.sentDate"); // output 1

Function Used

function setValue(array &$data, $path, $value) {
    $temp = &$data;
    foreach(explode(".", $path) as $key) {
        $temp = &$temp[$key];
    }
    $temp = $value;
}

function getValue($data, $path) {
    $temp = $data;
    foreach(explode(".", $path) as $ndx) {
        $temp = isset($temp[$ndx]) ? $temp[$ndx] : null;
    }
    return $temp;
}
Sunday, October 16, 2022
 
eoghanm
 
5

You can use a list of tuples (or list of lists). Like this:

Arr= [("Forename","Paul"),("Surname","Dinh")]
for Key,Value in Arr: 
    print Key,"=",Value

Forename = Paul
Surname = Dinh

you can make a dictionary out of this with:

Dictionary=dict(Arr)

And the correctly sorted keys like this:

keys = [k for k,v in Arr]

Then do this:

for k in keys: print k,Dictionary[k]

but I agree with the comments on your question: Would it not be easy to sort the keys in the required order when looping instead?

EDIT: (thank you Rik Poggi), OrderedDict does this for you:

od=collections.OrderedDict(Arr)
for k in od: print k,od[k]
Monday, November 28, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :