/    Sign up×
Community /Pin to ProfileBookmark

Build an array from a string

I have two test arrays below “$companydocs” and “$companydocs2”. I am trying to work on “$companydocs2” and build an array from a string based on a mysql query.

[B]Why isn’t my array data from $companydocs2 matching up with $companydocs when I var_dump() both of them?[/B] Is there a better way to dynamically build a multidimensional array?

[code=php]
// this is what I want
$companydocs = Array();
$companydocs = array
(
“date1″=>array( “presentation”=>array(“http://yahoo.com1”), “execsum”=>array(“http://google.com2”), “ppt”=>array(“http://cnn.com3”)),
“date2″=>array( “presentation”=>array(“http://yahoo.com4”), “execsum”=>array(“http://google.com5”)),
“date3″=>array( “execsum”=>array(“http://google.com6”))
);

// need help below building an array to match the output from the above array
$companydocs2 = Array();
$companydocs2 .= ‘array(‘;
$companydocs2 .= ‘”date1″=>array( “presentation”=>array(“http://yahoo.com1”), “execsum”=>array(“http://google.com2”), “ppt”=>array(“http://cnn.com3”)),’;
$companydocs2 .= ‘”date2″=>array( “presentation”=>array(“http://yahoo.com4”), “execsum”=>array(“http://google.com5”)),’;
$companydocs2 .= ‘”date3″=>array( “execsum”=>array(“http://google.com6”))’;
$companydocs2 .= ‘)’;
// end help

var_dump($companydocs);
echo “<hr />”;
var_dump($companydocs2);[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarNov 27.2008 — strings != arrays

that's why they arn't matching up, try:
[code=php]
$companydocs2 = array();
$companydocs2["date1"] = array(
"presentation"=>array("http://yahoo.com1"),
"execsum"=>array("http://google.com2"),
"ppt"=>array("http://cnn.com3")
)
$companydocs2["date2"]= array(
"presentation"=>array("http://yahoo.com4"),
"execsum"=>array("http://google.com5")
);
$companydocs2["date3"]=array(
"execsum"=>array("http://google.com6")
);
[/code]
Copy linkTweet thisAlerts:
@ripcurlksmauthorNov 27.2008 — eww.. I get it now. thanks
Copy linkTweet thisAlerts:
@ripcurlksmauthorNov 27.2008 — Ok I get how to structure an array, but how do you build a multidimensional array from a MySQL loop? I need to build the nested arrays based on what the database spits out.. meaning some array keys will have one element nested in them, and others will have either two or three keys nested within them.

It seems I still have to build the array using a string first, and then convert it some how to an array?

Here is what I have so far:
[code=php] dbConnect();
$sql = "SELECT * FROM emt_presentation WHERE emt_id='1' AND status='published'";
$result = mysql_query($sql) or die(mysql_error());


$companydocs = Array();

while($row = mysql_fetch_assoc($result)){

$source = $row['source_id'];
$date = $row['date'];
$emt_url = $row['emt_url'];
$emt_ppt = $row['emt_ppt'];
$exec_url = $row['exec_url'];


$string = '$companydocs["$date - $source"] = array(';

// this is a unique content type, if it returns a value, i want to include it in the array
if(!$emt_url == NULL){
$string .= '"presentation"=>array("$emt_url")';
}

// this is a unique content type, if it returns a value, i want to include it in the array
if(!$emt_ppt == NULL){
$string .= '"ppt"=>array("$emt_ppt")';
}

// this is a unique content type, if it returns a value, i want to include it in the array
if(!$exec_url == NULL){
$string .= '"execsum"=>array("$exec_url")';
}


$string .= ')';

echo $string;

}

var_dump($companydocs); [/code]
Copy linkTweet thisAlerts:
@scragarNov 27.2008 — [code=php]
dbConnect();
$sql = "SELECT * FROM emt_presentation WHERE emt_id='1' AND status='published'";
$result = mysql_query($sql) or die(mysql_error());


$companydocs = Array();

while($row = mysql_fetch_assoc($result)){
$source = $row['source_id'];
$date = $row['date'];
$emt_url = $row['emt_url'];
$emt_ppt = $row['emt_ppt'];
$exec_url = $row['exec_url'];



$tmpArr = array();// make a temp array

if( ! is_null($emt_url))// if it's not null
$tmpArr["presentation"] = array($emt_url);// add to the array
if( ! is_null($emt_ppt))
$tmpArr["ppt"] = array($emt_ppt);
if( ! is_null($exec_url))
$tmpArr["exec_sum"] = array($exec_url);

$companydocs["$date - $source"] = $tmpArr;// and then make the temp array a part of the whole.
}

unset($row, $tmpArray);// ram used by arrays adds up fairly quickly, so if you aren't using them again and it's not in a function drop it when you are done.

var_dump($companydocs);
[/code]
×

Success!

Help @ripcurlksm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...