Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a simple array without key from an array key=>value?

Tags:

arrays

php

list

key

I have an array like this : array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); and would like to get only my values, for example here :

simpleTab=array("35","37","43");

or otherwise like this, it should be better for me to get a list :

simpleList=["35";"37";"43"]

I'm creating a function because I'll need it several times so here it is :

$simpleArray=array(); //don't know if this should be array, i'd like to have a list

foreach($valueAssoc as $key => $value{//array_push($simpleArray,$value);//NO it returns an array with keys 0 , 1 , etc
    //$simpleArray[]=$value; //same ! I don't want any keys

    //I want only an array without any index or key "tableau indexé sans clef"
    echo $value;
    //would like to have some method like this :
    $simpleArray.add($value);//to add value in my list -> can't find the php method
like image 481
user9417455 Avatar asked Dec 01 '22 14:12

user9417455


1 Answers

If you want the without Key You should use array_values() and json_encode()(It means convert to string) the array like

$arr = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
print_r(json_encode(array_values($arr)));

OutPut:

["35","37","43"]
like image 135
Nawin Avatar answered Dec 03 '22 04:12

Nawin