note 56913 added to function.array-push

From: Date: Mon, 19 Sep 2005 12:33:57 +0000
Subject: note 56913 added to function.array-push
Groups: php.notes 
Request: Send a blank email to php-notes+get-95406@lists.php.net to get a copy of this message
Looking for a way to push data into an associative array and frustrated to know that array_push() can't do the job ? here's my Scenario : ------------------- I need to relate system command output into an associative array like these : [sge@digital_db work]$ /usr/local/apache/htdocs/work/qhost.sh -h t1 -F | awk '{if(NR>4) print $1}' | sed 's/hl://g' arch=lx24-amd64 num_proc=2.000000 mem_total=3.808G swap_total=3.907G virtual_total=7.715G load_avg=0.000000 load_short=0.000000 load_medium=0.000000 load_long=0.000000 mem_free=3.510G swap_free=3.907G virtual_free=7.417G mem_used=305.242M swap_used=0.000 virtual_used=305.242M cpu=0.000000 np_load_avg=0.000000 np_load_short=0.000000 np_load_medium=0.000000 np_load_long=0.000000 how I did it : <? php # get into the system command output $assoc_cmd =$work_dir/qhost.sh -h $host_resource -F | awk '{if(NR>4) print $1}'| sed 's/hl://g' ; # split the "\n" character $assoc_row = explode("\n", chop($assoc_cmd)); # get the index row $idx_row = count($assoc_row) - 1 ; # initialize the associative array $host_res_array = array(); for ($i = 0 ; $i<= $idx_row ; $i++) { # get params & values list($host_param,$host_val) = explode("=",$assoc_row[$i]); # populate / push data to assoc array $host_res_array[$host_param]= $host_val ; } echo "<pre> Architecture : </pre>\n" ; echo $host_res_array['arch'] ; echo "<pre> Mem Total : </pre>\n" ; echo $host_res_array['mem_tot']; ?> Hope this helps ! :) ---- Manual Page -- http://www.php.net/manual/en/function.array-push.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56913 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56913&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56913&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56913&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56913&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56913&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56913&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56913&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56913&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95406) next »